package com.example.smartall.ui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smartall.R;
import com.example.smartall.adapter.PervasiveAdapter;
import com.example.smartall.api.Api;
import com.example.smartall.api.ApiConfig;
import com.example.smartall.api.NetCallBack;
import com.example.smartall.diaglog.LoadedDialog;
import com.example.smartall.entity.HouseBean;
import com.example.smartall.entity.JobAdvertiseBean;
import com.example.smartall.ui.base.BaseActivity;
import com.example.smartall.utils.Constant;
import com.example.smartall.view.SListView;
import com.google.gson.Gson;
import com.youth.banner.Banner;

import java.util.List;

/**
 * @Author: abbas
 * @CreateDate: 2022/2/7
 * @Description: 找房子主页面，页面包括顶部导航栏目、宣传幻灯片、搜索、功能分类和房源展示板块。
 *               具有返回按钮，点击返回按钮可以返回智慧城市主页面。
 *               下方搜索在页面搜索框下面显示4大分类按钮，分别为：二手、租房、楼盘、中介，并图文显示。
 */
public class HouseActivity extends BaseActivity implements View.OnClickListener {

    private ImageView imBack;
    private TextView tvTitle;
    private Banner banner;
    private EditText etSearch;
    private Button btSearch;
    private Button btSecondHand;
    private Button btTenement;
    private Button btHouse;
    private Button btIntermediary;
    private TextView tvNone;
    private SListView lvHouse;

    private List<HouseBean.RowsBean> houseBeans;
    private ListAdapter adapter;

    private MHandler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_house);

        handler = new MHandler();

        init();
        initEvent();
        getHouseList("");
    }

    @Override
    public void init() {
        imBack = (ImageView) findViewById(R.id.im_back);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        banner = (Banner) findViewById(R.id.banner);
        etSearch = (EditText) findViewById(R.id.et_search);
        btSearch = (Button) findViewById(R.id.bt_search);
        btSecondHand = (Button) findViewById(R.id.bt_second_hand);
        btTenement = (Button) findViewById(R.id.bt_tenement);
        btHouse = (Button) findViewById(R.id.bt_house);
        btIntermediary = (Button) findViewById(R.id.bt_intermediary);
        tvNone = findViewById(R.id.tv_none);
        lvHouse = (SListView) findViewById(R.id.lv_house);

        tvTitle.setText("找房子");
    }

    @Override
    public void initEvent() {
        imBack.setOnClickListener(this);
        btSearch.setOnClickListener(this);
        btSecondHand.setOnClickListener(this);
        btTenement.setOnClickListener(this);
        btHouse.setOnClickListener(this);
        btIntermediary.setOnClickListener(this);

        lvHouse.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(HouseActivity.this, HouseDetailsActivity.class);
                intent.putExtra("houseId", houseBeans.get(i).getId());
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.im_back:
                HouseActivity.this.finish();
                break;
            case R.id.bt_search:
                getHouseList("?sourceName=" + etSearch.getText());
                break;
            case R.id.bt_second_hand:
            case R.id.bt_tenement:
            case R.id.bt_house:
            case R.id.bt_intermediary:
                getHouseList("?houseType="+((Button)view).getText()); //测试没问题
                break;
        }
    }

    private void getHouseList(String target) {
        LoadedDialog dialog = new LoadedDialog(HouseActivity.this);
        dialog.show();
        Api.config(ApiConfig.HOUSE, null).getRequest(new NetCallBack() {
            @Override
            public void onSuccess(String res) {
                dialog.dismiss();
                HouseBean bean = new Gson().fromJson(res, HouseBean.class);
                if (bean != null && bean.getCode() == 200) {
                    houseBeans = bean.getRows();
                    handler.sendEmptyMessage(0x01);
                }
            }

            @Override
            public void onFailure(Exception e) {
                dialog.dismiss();
            }
        }, target);
    }

    class MHandler extends Handler {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0x01:
                    if (adapter == null) {
                        adapter = new ListAdapter(houseBeans);
                        lvHouse.setAdapter(adapter);
                    } else {
                        adapter.setData(houseBeans);
                    }
                    //若没有查询到数据 显示未查询到数据
                    if (houseBeans == null || houseBeans.size() < 1) {
                        tvNone.setVisibility(View.VISIBLE);
                    } else {
                        tvNone.setVisibility(View.GONE);
                    }
                    break;
            }
        }
    }

    class ListAdapter extends PervasiveAdapter<HouseBean.RowsBean> {

        public ListAdapter(List<HouseBean.RowsBean> data) {
            super(data);
        }

        @Override
        protected void setUI(ViewHolder holder, int position, Context context) {
            ImageView img = (ImageView) holder.getItemView(R.id.img);
            TextView title = (TextView) holder.getItemView(R.id.title);
            TextView subtitle = (TextView) holder.getItemView(R.id.subtitle);
            TextView content = (TextView) holder.getItemView(R.id.content);

            Glide.with(HouseActivity.this)
                    .load(ApiConfig.BASE_URL + houseBeans.get(position).getPic())
                    .into(img);
            title.setText(houseBeans.get(position).getSourceName());
            subtitle.setText(houseBeans.get(position).getAreaSize() + "");
            content.setText("价格：" + houseBeans.get(position).getPrice() +
                    "\n" + houseBeans.get(position).getDescription());
        }

        @Override
        protected int getLayoutId() {
            return R.layout.common_list_item;
        }
    }
}